﻿using DreamCMS.BLL;
using DreamCMS.Entity.Common;
using DreamCMS.WebCore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DreamCMS.Entity
{
    public class PostInfo : IComparable<PostInfo>
    {
        //实现CompareTo接口，就能在PostInfo类型的数组上调用Sort方法进行排序。元素的比较原则就根据以下规则制定。
        public int CompareTo(PostInfo other)
        {
            return other.PostId.CompareTo(this.PostId);
        }
        private long postId = 0;  //文章id.默认设置为0.因为在DAO里面有根据0判断是否有ID。（其实不用指定，c#默认字段就是0）
        private long categoryId; //栏目类别ID
        private string name=string.Empty; //文章标题
        private string summary=string.Empty; //文章摘要
        private string content=string.Empty;  //文章内容
        private string customurl=string.Empty;  //自定义URL
        private long userid;  //用户ID
        private PostCommentStatus commentstatus;  //评论状态

        private long totalcomment;  //评论数量
        private long hits;  //浏览数量（点击数）
        private string tag=string.Empty; //标签
        private PostUrlFormate urltype;  //URL格式类型
        private string template = "post.html";  //文章页模板名
        private PostRecommend recommend; //是否推荐
        private PostStatus status; //文章状态
        private PostTopStatus topstatus; //是否置顶
        private PostHideStatus hidestatus;  //是否隐藏
        private DateTime createdate=DateTime.Now; //创建时间
        private DateTime updatedate; //更新时间
        public string imageUrl=string.Empty; //图像名称，请在数据库中使用这个

        /// <summary>
        /// 缩略图地址
        /// </summary>
        public string ImageUrl
        {
            get
            {
                string url = string.Empty;
                if (string.IsNullOrEmpty(imageUrl))
                {
                    url = ConfigHelper.SiteRootPath + "common/images/no.jpg";
                }
                else
                {
                    url = ConfigHelper.SiteRootPath + "uploads/thumb/" + imageUrl;
                }

                return url;
            }
            set { imageUrl = value; }
        }

        /// <summary>
        /// 本文章地址
        /// </summary>
        public string Url
        {
            get
            {
                string url = null;
                url = string.Format("{0}post.aspx?name={1}",ConfigHelper.SiteRootPath, !string.IsNullOrEmpty(this.Slug) ? StringHelper.UrlEncode(this.Slug) : this.PostId.ToString());
                return url;
            }
        }

        /// <summary>
        /// 本网站的a链接字符串
        /// </summary>
        public string Link
        {
            get { return string.Format("<a href=\"{0}\">{1}</a>", this.Url, this.Title); }
        }

        /// <summary>
        /// 订阅URL地址
        /// </summary>
        public string FeedUrl
        {
            get
            {
                return string.Format("{0}rss.aspx?type=post&id={1}", ConfigHelper.SiteRootPath, this.PostId);
            }
        }

        /// <summary>
        /// 订阅URL的a连接
        /// </summary>
        public string FeedLink
        {
            get
            {
                return string.Format("<a href=\"{0}\" title=\"订阅:{1} 的评论\">订阅</a>", this.FeedUrl, this.Title);
            }
        }

        /// <summary>
        /// 文章详情
        /// 根据设置而定,可为摘要,正文，200字或者标题
        /// </summary>
        public string Detail
        {
            get
            {
                switch ((ContentShowType)SiteSettingManager.GetSiteSettingInfo().PostShowType)
                {
                    case ContentShowType.OnlyTitle:
                        {
                            return this.name;
                        }
                    case ContentShowType.OnlySummary:
                    default:
                        {
                            return StringHelper.RemoveHtml(this.summary);
                        }
                    case ContentShowType.MainContent:
                        {
                            return this.content;
                        }
                    case ContentShowType.Begin200:
                        {
                            return StringHelper.CutString(StringHelper.RemoveHtml(this.content), 200, "......");
                        }
                }
            }
        }

        /// <summary>
        /// 作者
        /// </summary>
        public UserInfo Author
        {
            get
            {
                UserInfo user = UserManager.GetOneUser(this.UserId);
                if (user != null)
                {
                    return user;
                }
                return new UserInfo();

            }
        }


        /// <summary>
        /// 下一篇文章(新)
        /// </summary>
        public PostInfo Next
        {
            get
            {
                List<PostInfo> list = PostManager.GetPostList();
                PostInfo post = list.Find(delegate(PostInfo p) { return p.HideStatus == PostHideStatus.NoHide && p.Status == PostStatus.Published && p.PostId > this.PostId; });
                return post != null ? post : new PostInfo();
            }
        }

        /// <summary>
        /// 上一篇文章(旧)
        /// </summary>
        public PostInfo Previous
        {
            get
            {
                List<PostInfo> list = PostManager.GetPostList();
                PostInfo post = list.Find(delegate(PostInfo p) { return p.HideStatus == PostHideStatus.NoHide && p.Status == PostStatus.Published && p.PostId < this.PostId; });
                return post != null ? post : new PostInfo();
            }
        }



        /// <summary>
        /// 文章ID
        /// </summary>
        public long PostId
        {
            set { postId = value; }
            get { return postId; }
        }

        /// <summary>
        /// 分类ID
        /// </summary>
        public long CategoryId
        {
            set { categoryId = value; }
            get { return categoryId; }
        }
        public CategoryInfo CatInfo
        {
            get
            {
                return CategoryManager.GetCatById(this.CategoryId);
            }
        }
        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            set { name = value; }
            get { return name; }
        }
        /// <summary>
        /// 别名
        /// </summary>
        public string Slug
        {
            set { customurl = value; }
            get { return customurl; }
        }

        /// <summary>
        /// 摘要
        /// </summary>
        public string Summary
        {
            set { summary = value; }
            get { return summary; }
        }
        /// <summary>
        /// 正文
        /// </summary>
        public string Content
        {
            set { content = value; }
            get { return content; }
        }

        /// <summary>
        /// 用户ID
        /// </summary>
        public long UserId
        {
            set { userid = value; }
            get { return userid; }
        }
        /// <summary>
        /// 是否允许评论
        /// </summary>
        public PostCommentStatus CommentStatus
        {
            set { commentstatus = value; }
            get
            {
                return commentstatus;
            }
        }

        /// <summary>
        /// 点击数
        /// </summary>
        public long ViewCount
        {
            set { hits = value; }
            get { return hits; }
        }
        /// <summary>
        /// 文章表的标签字段内容（只是记录了一些标签ID号）
        /// </summary>
        public string Tag
        {
            set { tag = value; }
            get { return tag; }
        }
        /// <summary>
        /// 文章对应的真实标签（用Tag标签ID字符串来调用一个函数转换为真实的标签）
        /// </summary>
        public List<TagInfo> Tags
        {
            get
            {
                string temptags = this.Tag.Replace("{", "").Replace("}", ",");

                if (temptags.Length > 0)
                {
                    temptags = temptags.TrimEnd(',');
                }

                return TagManager.GetTagList(temptags);
            }
        }

        /// <summary>
        /// URL类型,见枚举PostUrlFormat
        /// </summary>
        public PostUrlFormate UrlFormat
        {
            set { urltype = value; }
            get { return urltype; }
        }

        /// <summary>
        /// 模板
        /// </summary>
        public string Template
        {
            set { template = value; }
            get { return template; }
        }

        /// <summary>
        /// 推荐
        /// </summary>
        public PostRecommend Recommend
        {
            set { recommend = value; }
            get { return recommend; }
        }


        /// <summary>
        /// 文章是否是发布状态
        /// </summary>
        public PostStatus Status
        {
            set { status = value; }
            get { return status; }
        }


        /// <summary>
        /// 置顶
        /// </summary>
        public PostTopStatus TopStatus
        {
            set { topstatus = value; }
            get { return topstatus; }
        }

        /// <summary>
        /// 隐藏于列表
        /// </summary>
        public PostHideStatus HideStatus
        {
            set { hidestatus = value; }
            get { return hidestatus; }
        }
        /// <summary>
        /// 添加时间
        /// </summary>
        public DateTime CreateDate
        {
            set { createdate = value; }
            get { return createdate; }
        }
        /// <summary>
        /// 最后编辑时间
        /// </summary>
        public DateTime UpdateDate
        {
            set { updatedate = value; }
            get { return updatedate; }
        }
        /// <summary>
        /// 评论数
        /// </summary>
        public long CommentCount
        {
            get { return this.totalcomment; }
            set { this.totalcomment = value; }
        }
    }
}
